package com.sevenGroup.pawnshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sevenGroup.pawnshop.Utils.ParameterCalibrationUtil;
import com.sevenGroup.pawnshop.Utils.Result;
import com.sevenGroup.pawnshop.controller.form.FirstTypePropertiesForm;
import com.sevenGroup.pawnshop.controller.form.PropertiesForm;
import com.sevenGroup.pawnshop.controller.form.SelectPropertiesForm;
import com.sevenGroup.pawnshop.controller.form.pageForm;
import com.sevenGroup.pawnshop.controller.info.PropertiesInfo;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.FirstType;
import com.sevenGroup.pawnshop.model.Properties;
import com.sevenGroup.pawnshop.service.PropertiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-18
 */
@RestController
@RequestMapping("/properties")
@Api(tags = "处理属性相关信息的接口")
public class PropertiesController {

    @Autowired
    private PropertiesService propertiesService;

    //新增属性
    @PostMapping("saveProperties")
    @ApiOperation("新增属性")
    @PreAuthorize("hasAnyAuthority('boss')")
    public Result saveProperties(@ApiParam("需要传入的属性对象")@Valid @RequestBody PropertiesForm propertiesForm, BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        propertiesService.saveProperties(propertiesForm);
        return Result.success(null);
    }

    //根据id删除
    @ApiOperation("根据id删除")
    @PreAuthorize("hasAnyAuthority('boss')")
    @PostMapping("delPropertiesByPropertiesId")
    public Result delPropertiesByPropertiesId(@ApiParam("属性id")Integer propertiesId) throws sevenGroupException {
        propertiesService.delPropertiesByPropertiesId(propertiesId);
        return Result.success(null);
    }

    //多选，根据集合删除
    @ApiOperation("多选，根据集合删除")
    @PreAuthorize("hasAnyAuthority('boss')")
    @PostMapping("delPropertiesByList")
    public Result delPropertiesByList(@ApiParam("属性id集合")@RequestBody List<Integer> list) throws sevenGroupException {
        propertiesService.delPropertiesByList(list);
        return Result.success(null);
    }

    //更新属性
    @ApiOperation("更新属性")
    @PreAuthorize("hasAnyAuthority('boss')")
    @PostMapping("updateProperties")
    public Result updateProperties(@ApiParam("需要传入的属性对象")@RequestBody @Valid PropertiesForm propertiesForm,BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        propertiesService.updateProperties(propertiesForm);
        return Result.success(null);
    }

    //查询所有属性
    @PostMapping("selectAllProperties")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    @ApiOperation("查询所有属性")
    public Result selectAllProperties(@ApiParam("传入的分页对象")@Valid @RequestBody pageForm pageForm,BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        IPage<PropertiesInfo> selectAllProperties = propertiesService.selectAllProperties(pageForm);
        return Result.success(selectAllProperties);
    }

    //根据商品id来查
    @GetMapping("selectPropertiesByGoodId/{goodId}")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    @ApiOperation("根据商品id来查")
    public Result selectPropertiesByGoodId(@ApiParam("传入商品id")@PathVariable Integer goodId) throws sevenGroupException {
        List<PropertiesInfo> properties = propertiesService.selectPropertiesByGoodId(goodId);
        return Result.success(properties);
    }

    //根据条件模糊查询属性
    @PostMapping("selectPropertiesByAll")
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("根据条件模糊查询属性")
    public Result selectPropertiesByPropertiesName(@ApiParam("模糊查询属性传入的条件") @RequestBody SelectPropertiesForm selectPropertiesForm) throws sevenGroupException {
        IPage<PropertiesInfo> propertiesInfoIPage = propertiesService.selectPropertiesByPropertiesName(selectPropertiesForm);
        return Result.success(propertiesInfoIPage);
    }

    @ApiOperation("根据商品大类查询属性集合")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    @GetMapping("selectPropertiesByFirstTypeId/{firstTypeId}")
    public Result selectPropertiesByFirstTypeId(@PathVariable Integer firstTypeId) throws sevenGroupException {
        List<PropertiesInfo> propertiesInfos = propertiesService.selectPropertiesByFirstTypeId(firstTypeId);
        return Result.success(propertiesInfos);
    }

    @ApiOperation("更新大类属性中间表")
    @PostMapping("updateFirstTypeProperties")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    public Result updateFirstTypeProperties(@RequestBody @Valid FirstTypePropertiesForm firstTypePropertiesForm,BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        propertiesService.updateFirstTypeProperties(firstTypePropertiesForm);
        return Result.success(null);
    }

    @ApiOperation("根据属性id查询大类集合")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    @GetMapping("selectFirstTypesByProperties/{propertiesId}")
    public Result selectFirstTypesByProperties(@PathVariable Integer propertiesId) throws sevenGroupException {
        List<FirstType> firstTypes = propertiesService.selectFirstTypesByProperties(propertiesId);
        return Result.success(firstTypes);
    }

}

